//
//  CaledarWeatherView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/3/22.
//

import UIKit
import SnapKit
class CaledarWeatherView: View {
    let todayTitle = ContentLabel(R.font.interMedium(size: 12))
    let weekDayText = ContentLabel(R.font.interBold(size: 15),hexStr: "#292D32B2")
    let dateLabel = ContentLabel(R.font.interRegular(size: 12),hexStr: "#292D3266")
    let tempretureLabel = ContentLabel(R.font.interBold(size: 20))
    let weatherImageView = UIImageView()
    override func setUpView() {
        self.drawCorner(byRoundingCorners: .allCorners, backColorHexStr: "#FFFFFFFF")
        //today title
        self.addSubview(todayTitle)
        todayTitle.snp.makeConstraints { make in
            make.left.top.equalTo(20)
            make.size.greaterThanOrEqualTo(0)
        }
        //weekDayText
        self.addSubview(weekDayText)
        weekDayText.snp.makeConstraints { make in
            make.top.equalTo(todayTitle.snp.bottom).offset(16)
            make.left.equalTo(todayTitle)
            make.size.greaterThanOrEqualTo(0)
        }
        //date label
        self.addSubview(dateLabel)
        dateLabel.snp.makeConstraints { make in
            make.top.equalTo(weekDayText.snp.bottom).offset(4)
            make.left.equalTo(todayTitle)
            make.size.greaterThanOrEqualTo(0)
            make.bottom.equalTo(-20)
        }
        //tempretureLabel
        self.addSubview(tempretureLabel)
        tempretureLabel.snp.makeConstraints { make in
            make.bottom.equalTo(-28)
            make.right.equalTo(-10)
            make.size.greaterThanOrEqualTo(0)
        }
        //weatherImageView
        self.addSubview(weatherImageView)
        weatherImageView.snp.makeConstraints { make in
            make.centerY.equalTo(tempretureLabel)
            make.right.equalTo(tempretureLabel.snp.left).offset(-5)
            make.size.greaterThanOrEqualTo(0)
        }
    }
    func setData(_ model: DayWeatherModel, date: Date){
        self.dateLabel.text = DateUtil().convertDateStrToDateStr(dateStr: model.date, oldFormat: "yyyy-MM-dd", newFormat: "dd'th' MMM, yyyy")
        self.weekDayText.text = "(\(DateUtil().getDatetimeStr(date, format: "EEEE")))"
        //
        let gregorian = Calendar(identifier: .gregorian)
        if gregorian.isDateInToday(date){//今天显示当前时间的问题
            todayTitle.text = "Today’s Weather"
            let hours = model.hour
            let currentHour = DateUtil().getDatetimeStr(Date(), format: "HH:00",timeZone: TimeZone.current)
            if let hoursModel = hours.first(where: {$0.time.contains(currentHour)}){
                self.tempretureLabel.text = hoursModel.temp_c + "°C"
                
                self.weatherImageView.image = UIImage(named: hoursModel.condition_icon_seq)
            }
        }else{//非当天显示，最高温度和最低温度
            todayTitle.text = "Weather Forecast"
            self.tempretureLabel.text = model.mintemp_c + "°C - " + model.maxtemp_c + "°C"
            self.weatherImageView.image = UIImage(named: model.condition_icon_seq)
        }
    }
}

